/**
 * Created by ruiyuan on 15-10-8.
 */
var Task = mongoose.model('user_task');
var moment = require('moment');
var toolUtil = require('../common/utils/toolUtil');
    /*  宝宝树66小区二次改造    mod   dayu  20170723   start   */
//var scoreService = require('../services/score-Service');
    var scoreService = require('../services/score-service');
    /*  宝宝树66小区二次改造    mod   dayu  20170723   end   */

exports.getNoviceTaskDetailService = function (user, callback) {

    Task.findOne({
        user: user._id,
        deadline: {$gt: new Date()},
        type: 'novice'
    }, 'deadline reward name subTask', function (err, result) {
        if (err) {
            callback(err)
        } else {
            if (result == null) {
                callback({message: '无新手任务'})
            } else {
                result._doc.diffDay = moment(result.deadline).diff(moment(), 'day') + 1;
                var i = 0;
                result.subTask.forEach(function (task) {
                    if (task.status) {
                        i++;
                    }
                });
                result._doc.progress = i == 0 ? 0 : (i / result.subTask.length).toFixed(2) * 100;
                callback(null, result)
            }
        }
    });
};

exports.finishTaskService = function (userId, action, callback) {
    Task.findOneAndUpdate({
        user: userId,
        type: 'novice',
        deadline: {$gt: new Date()},
        subTask: {$elemMatch: {action: action, status: false}}
    }, {'subTask.$.status': true}, function (err, result) {
        if (result != null) {
            var flag = true;
            var count = 0;
            result.subTask.forEach(function (task) {
                if (!task.status) {
                    flag = false;
                } else {
                    count++;
                }
            });
            if (flag) {
                toolUtil.hessianManualAddScore(userId, 20, result.reward, result.name, function (value) {
                    if (value != null) {

                        var ot = 14;
                        /*  宝宝树66小区二次改造    mod   chenf  20170717   start   */
                        /*var desc = '恭喜你！成功完成新手任务,获得' + result.reward + '积分！积分可以兑换礼物哦';*/
                        var desc = '恭喜完成全部新手任务，小6为你准备了不菲的奖励，快去抽奖吧！';
                        /*  宝宝树66小区二次改造    mod   chenf  20170717   end   */

                        toolUtil.putMessageBusinessMQ({msgType: 'finish_task', uid: userId, desc: desc, openType: ot});

                        redisClient.HSET('finish:novice:task', userId, JSON.stringify({
                            uid: userId,
                            desc: desc,
                            openType: ot,
                            date: new Date().getTime()
                        }));

                        callback(null, result.reward);
                    } else {
                        callback();
                    }
                });
            } else {
                var ot = 13;
                /*  宝宝树66小区二次改造    mod   chenf  20170717   start   */
                /*var desc = '好样的, 你完成了一项新手任务,去看看其他任务吧! ';*/
                var desc = '新技能get！还有重要任务等你攻破哦，进行下一任务！';
                /*  宝宝树66小区二次改造    mod   chenf  20170717   end   */

                redisClient.HSET('finish:novice:task', userId, JSON.stringify({
                    uid: userId,
                    desc: desc,
                    openType: ot,
                    date: new Date().getTime()
                }));

                toolUtil.putMessageBusinessMQ({msgType: 'finish_task', uid: userId, desc: desc, openType: ot});
                callback();
            }
        } else {
            callback();
        }
    })
};